SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors
GO
CREATE TABLE #tmpErrors (Error int)
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
GO

/*===================================BEGIN=================================*/

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[TEAM_MEMBER](
	[ID] [uniqueidentifier] NOT NULL,
	[TEAM_ID] [uniqueidentifier] NOT NULL,
	[USER_ID] [uniqueidentifier] NOT NULL,
	[JOIN_DATE] [datetime] NULL,
	[ACTIVE] [bit] NOT NULL,
 CONSTRAINT [PK_TEAM_MEMBER] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[TEAM_MEMBER]  WITH CHECK ADD  CONSTRAINT [FK_TEAM_MEMBER_TEAMS] FOREIGN KEY([TEAM_ID])
REFERENCES [dbo].[TEAMS] ([ID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[TEAM_MEMBER] CHECK CONSTRAINT [FK_TEAM_MEMBER_TEAMS]
GO

ALTER TABLE [dbo].[TEAM_MEMBER]  WITH CHECK ADD  CONSTRAINT [FK_TEAM_MEMBER_USERS] FOREIGN KEY([USER_ID])
REFERENCES [dbo].[USERS] ([ID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[TEAM_MEMBER] CHECK CONSTRAINT [FK_TEAM_MEMBER_USERS]
GO

/*===================================END=================================*/


IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO

IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT>0 BEGIN
PRINT 'The database update succeeded'

-- Record the script update
INSERT INTO dbo.[SchemaChanges]
       ([MajorReleaseNumber]
       ,[MinorReleaseNumber]
       ,[BuildReleaseNumber]
       ,[RevisionReleaseNumber]
       ,[ScriptName]
       ,[DateApplied]
       ,[Description])
VALUES
       ('1'
       ,'00'
       ,'01'
       ,'007'
       ,'Upgrade-1.00.01.007.sql'	   
       ,GETDATE()
	   ,'Create table TEAM_MEMBER')
	   COMMIT TRANSACTION
END
ELSE PRINT 'The database update failed'
GO
DROP TABLE #tmpErrors
GO        

